package org.erp.product.controller;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.product.infrastructure.convert.ProductInfoConvert;
import org.erp.product.domain.bo.ConnectToProductInfoAddBo;
import org.erp.product.domain.bo.ProductInfoAddBo;
import org.erp.product.domain.bo.ProductInfoQueryBo;
import org.erp.product.domain.vo.ProductInfoVo;
import org.erp.product.service.ProductInfoService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 产品控制层
 *
 * @author libin
 * @version 1.0
 * @description: 产品控制层
 * @date 2025/11/13 18:11
 **/
@Log4j2
@RestController
@AllArgsConstructor
@RequestMapping("/product")
public class ProductInfoController {

    private final ProductInfoService productInfoService;

    private final ProductInfoConvert productInfoConvert;


    private ConnectToProductInfoAddBo createTestData(Integer start, Integer end) {
        ConnectToProductInfoAddBo bo = new ConnectToProductInfoAddBo();
        bo.setTransferType(1);
        bo.setOperationId(1L);
        bo.setOperationName("张三");
        bo.setTransferId(2L);
        bo.setTransferName("李四");

        List<ProductInfoAddBo> productList = new ArrayList<>();
        String[] colors = {"白色", "黑色", "红色", "蓝色", "金色"};
        String[] storages = {"128G", "64G", "256G", "512G"};

        for (int i = start; i <= end; i++) { // 创建200条测试数据
            ProductInfoAddBo product = new ProductInfoAddBo();
            product.setStockId((long) i);
            product.setProductName("小米" + i);
            product.setBrandId(1L);
            product.setCategoryId(1L);
            product.setModelId((long) (i % 3 + 1));
            product.setSeriesId((long) (i % 3 + 1));
            product.setLevelId((long) (i % 3 + 1));
            product.setProductImei("IMEI" + (10000 + i));
            product.setProductCode("PC" + String.format("%06d", i));
            product.setProductColor(colors[i % colors.length]);
            product.setProductStorage(storages[i % storages.length]);
            product.setCostPrice(900 + i * 50);
            product.setDeptId(1L);
            product.setDeptName("维修");
            product.setTenantId(1L);
            product.setTenantName("小黑");
            product.setStoreId(1L);
            product.setStoreName("杭州仓");
            productList.add(product);
        }

        bo.setProductInfoAddBoList(productList);
        return bo;
    }

    /**
     * 产品列表
     *
     * @param productInfoQueryBo
     * @return
     */
    @PostMapping("/list")
    public Result<DataPageResp<ProductInfoVo>> pageList(@RequestBody ProductInfoQueryBo productInfoQueryBo) {
        PageQueryModel<ProductInfoVo> pageList = productInfoService.pageList(productInfoConvert.boConvertDto(productInfoQueryBo));
        return success(DataPageResp.of(pageList.getTotal(), pageList.getDataList()));
    }
}
